/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Acceso;

import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author alb3rto
 */
public class FormatUtility {

    public static String md5(String text) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] b = md.digest(text.getBytes());
            int size = b.length;
            StringBuilder h = new StringBuilder(size);

            for (int i = 0; i < size; i++) {
                int u = b[i] & 255;
                if (u < 16) {
                    h.append("0").append(Integer.toHexString(u));
                } else {
                    h.append(Integer.toHexString(u));
                }
            }
            return h.toString();
            //JOptionPane.showMessageDialog(null, "" + MD5, "Salida del Action", 1);
        } catch (Exception e) {
            System.out.println("Ha ocurrido un error " + e.getMessage());
            return null;
        }
    }

    /*
     * Metodo que valida que una cadena tenga u formato determinado
     * @param cadena: String a validar
     * @param type: Tipo de patron a validar
     * @return boolean
     */
    public static boolean validatePattern(String cadena, int type) {
        Matcher m;
        Pattern p;
        //(1=caracteres simples, 2=numeros, 3=caracteres, 4=email, 5=numeros o caracteres)
        switch (type) {
            case 1:
                p = Pattern.compile("[a-zA-Z]+");
                break;
            case 2:
                p = Pattern.compile("[0-9]*");
                break;
            case 3:
                p = Pattern.compile("[a-zA-Záéíóú ]*");
                break;
            case 4:
                p = Pattern.compile("^([0-9a-zA-Z]([_.w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-w]*[0-9a-zA-Z].)+([a-zA-Z]{2,9}.)+[a-zA-Z]{2,3})$");
                break;
            case 5:
                p = Pattern.compile("[a-zA-Z0-9._#& ]*");
                break;
            default:
                return false;
        }

        m = p.matcher(cadena);
        return m.matches();
    }
}
